//
//  CYAllVideoViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CYAllVideoViewController.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "CYAllVideoListTableViewCell.h"
#import "FitHeader.h"
#import "CYAllVideoListModel.h"
#import <MJRefresh.h>
#import "VideoListViewController.h"
#import <MBProgressHUD.h>
#import "Loading.h"
@interface CYAllVideoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)NSMutableArray *cateArray;
@property(nonatomic, strong)NSMutableArray *listArray;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, strong)Loading *load;
@end

@implementation CYAllVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"全部视频";
    self.navigationController.navigationBar.translucent = YES;

    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < 70; i++) {
        [array addObject:[NSString stringWithFormat:@"%ld", i + 1]];
    }
    _load = [[Loading alloc]initWithFrame:CGRectMake(0, 0, 375, 667) PicArray:array];
    [self.view addSubview:_load];
    
    _listArray = [NSMutableArray array];
    [self getData];
    _page += 0;
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page += 20;
        [self getData];
    }];
}

-(void)getData
{
    _cateArray = [NSMutableArray array];
    NSString *PostString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=f2aeeaf9a2763c6af1db790dcf7fb186&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C7C4F45AC-D834-40CC-8E57-1C73070C0250%7C28F136CD-C3C2-4542-AF9B-2BAD4F1067E6&format=json&loguid=&method=Info.getVideoList&nonce=1446722542&sessionid=1446722501&signmethod=md5&timestamp=1446722542&uuid=8aa885128b339761cd4b5fc380d868d6&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = [NSString stringWithFormat:@"cate_id=%@&limit=20&offset=%ld&sign=&uid=&uuid=8aa885128b339761cd4b5fc380d868d6", _IdString, _page];
    [HTTPMethod getDataByString:PostString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        
        NSDictionary *dic = [data objectForKey:@"result"];
        _cateArray = [dic objectForKey:@"cate"];
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dataDic in array) {
            CYAllVideoListModel *allVideo = [[CYAllVideoListModel alloc]init];
            [allVideo setValuesForKeysWithDictionary:dataDic];
            [_listArray addObject:allVideo];
        }
        [_tableView.footer endRefreshing];
        [_tableView reloadData];
        [_load removeFromSuperview];
    }];
}

-(void)loadView
{
    [super loadView];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * ht;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CYAllVideoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CYAllVideoListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.allVideo = _listArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoListViewController *vvc = [[VideoListViewController alloc]init];
    vvc.IdString = [_listArray[indexPath.row] RecipeId];
    vvc.titleString = [_listArray[indexPath.row] Title];
    [self.navigationController pushViewController:vvc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
